//
//  WYCPreviewViewController.m
//  掌上还
//
//  Created by 王晓民 on 2018/1/17.
//  Copyright © 2018年 中企互联. All rights reserved.
//

#import "WYCPreviewViewController.h"
#import "repayPlanViewCell.h"
#import "repayModel.h"
#import "amountModel.h"
@interface WYCPreviewViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong) NSString *total;
@property(nonatomic,strong)UILabel *Statement1;
@property(nonatomic,strong) UILabel *Repaymentlabel1;
@property(nonatomic,strong) NSString *planNo;
@property(nonatomic,strong)UIView  *introView;
@property(nonatomic,strong)UIView * showView;
@end

@implementation WYCPreviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   

    [self setupNav];
    [self setupTableView];
    [self setupbottomView];
    //网络请求
    [self loadplan];
    self.dataArray = [[NSMutableArray alloc] init];
    
    self.detailsArray = [[NSMutableArray alloc] init];
}

#pragma mark ------>>创建tableview
- (void)setupTableView{

    UIView  *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 80)];
    headview.backgroundColor = [UIColor whiteColor];
    headview.userInteractionEnabled = YES;
    UILabel *Statement = [[UILabel alloc] init];
    Statement.frame = CGRectMake(10,20, 100, 30);
    Statement.text = @"还款总金额:";
    Statement.textAlignment = NSTextAlignmentLeft;
    Statement.textColor = [UIColor blackColor];
    Statement.font = [UIFont systemFontOfSize:15];
    [headview addSubview:Statement];
    
    //还款日
    UILabel *Repaymentlabel = [[UILabel alloc] init];
    Repaymentlabel.frame = CGRectMake(10, 50, 100, 30);
    Repaymentlabel.text = @"总计手续费:";
    Repaymentlabel.textAlignment = NSTextAlignmentLeft;
    Repaymentlabel.textColor = [UIColor blackColor];
    Repaymentlabel.font = [UIFont systemFontOfSize:15];
    [headview addSubview:Repaymentlabel];
    
    //总金额
     _Statement1= [[UILabel alloc] init];
    _Statement1.frame = CGRectMake(ScreenWidth-120,20, 100, 30);
    _Statement1.textAlignment = NSTextAlignmentRight;
    _Statement1.textColor = [UIColor redColor];
    _Statement1.text = @"9000";
    _Statement1.font = [UIFont systemFontOfSize:15];
    
    [headview addSubview:_Statement1];
    
    //手续费
     _Repaymentlabel1 = [[UILabel alloc] init];
    _Repaymentlabel1.frame = CGRectMake(ScreenWidth-120, 50, 100, 30);
   _Repaymentlabel1.textAlignment = NSTextAlignmentRight;
    _Repaymentlabel1.textColor = [UIColor redColor];
    _Repaymentlabel1.font = [UIFont systemFontOfSize:15];
    _Repaymentlabel1.text = @"6.3";
    [headview addSubview:_Repaymentlabel1];

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth,ScreenHeight-64) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = WYCGlobalBg;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
   self.tableView.tableHeaderView = headview;
    [self.view addSubview:self.tableView];
    
    
}
//底部还款
- (void)setupbottomView{
    //底部view
    UIView * bottomView = [[UIView alloc] init];
    bottomView.frame = CGRectMake(0, ScreenHeight - 160 * px, ScreenWidth, 160 * px);
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    //重新创建计划
    UIButton * alreadylabel = [[UIButton alloc] init];
    alreadylabel.frame = CGRectMake(0, 0, bottomView.width / 2, 160 * px);
    alreadylabel.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    [alreadylabel setTitle:@"重新创建计划" forState:UIControlStateNormal];
    [alreadylabel setTitleColor:wycMain forState:UIControlStateNormal];
    [alreadylabel addTarget:self action:@selector(alreadylabel) forControlEvents:UIControlEventTouchUpInside];
    alreadylabel.titleLabel.font = [UIFont systemFontOfSize:BigFont];
    [bottomView addSubview:alreadylabel];
    
    

    //执行该计划
    UIButton * payofflabel = [[UIButton alloc] init];
    payofflabel.frame = CGRectMake(bottomView.width / 2, alreadylabel.y, alreadylabel.width, alreadylabel.height);
    payofflabel.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    [payofflabel setTitle:@"执行该计划" forState:UIControlStateNormal];
    payofflabel.backgroundColor = wycMain;
    [payofflabel addTarget:self action:@selector(payofflabel) forControlEvents:UIControlEventTouchUpInside];
    [payofflabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    payofflabel.titleLabel.font = [UIFont systemFontOfSize:BigFont];
    [bottomView addSubview:payofflabel];
    
}
//返回
-(void)alreadylabel
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)payofflabel
{
    WYCLog(@"_planNo%@",_planNo);

    if (_planNo.length == 0) {

    }
    else
    {
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"token"] = [WYCAccountTool unarchivetoken];
        params[@"planNo"] = _planNo;
        [HttpTool POST:epaymentplanbank params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            
            WYCLog(@"-----------responseobject%@",responseObject);
            NSLog(@"计划创建返回信息%@",[responseObject objectForKey:@"msg"]);
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"计划创建成功"];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
                [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
                [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
                UIViewController *viewCtl = self.navigationController.viewControllers[1];
                
                [self.navigationController popToViewController:viewCtl animated:YES];
                
            }
            else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"计划创建失败"];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
                [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
                [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
    }
    
}
- (void)setupNav {
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.title = @"预览消费计划";
    //右侧按钮
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"text" hight:@"text" target:self action:@selector(introduceBtn)];
}
-(void)introduceBtn
{
    //背景透明
    _introView = [[UIView alloc] init];
    _introView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    _introView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [[UIApplication sharedApplication].keyWindow  addSubview:_introView];
    
    //最底层
   _showView  = [[UIView alloc] init];
    _showView.frame = CGRectMake(40, ScreenHeight / 2 - 100, ScreenWidth - 80, 200);
    _showView.layer.cornerRadius = 5;
    _showView.layer.masksToBounds = YES;
    _showView.backgroundColor = [UIColor whiteColor];
    [_introView addSubview:_showView];
    //红色的view
    UIView * topView = [[UIView alloc] init];
    topView.frame = CGRectMake(0, 0, _showView.width, 40);
    topView.backgroundColor = wycMain;
    [_showView addSubview:topView];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:topView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5,5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = topView.bounds;
    maskLayer.path = maskPath.CGPath;
    topView.layer.mask = maskLayer;
    

    UILabel * showlabel = [[UILabel alloc] init];
    showlabel.frame = CGRectMake(0, 0, topView.width, topView.height);
    showlabel.text = @"手续费说明";
    showlabel.textColor = [UIColor whiteColor];
    showlabel.font = [UIFont boldSystemFontOfSize:16];
    showlabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:showlabel];
    
    
    UILabel * show = [[UILabel alloc] init];
    show.frame = CGRectMake(10, CGRectGetMaxY(topView.frame) + 20 * px, topView.width-10, 20);
    show.text = @"手续费由2部分组成";
    show.textColor = [UIColor grayColor];
    show.font = [UIFont systemFontOfSize:16];
    show.textAlignment = NSTextAlignmentLeft;
    [_showView addSubview:show];
    
    UILabel * show1 = [[UILabel alloc] init];
    show1.frame = CGRectMake(10, CGRectGetMaxY(show.frame) + 20 * px, topView.width-10, 20);
    show1.text = @"还款手续费=还款总金额*0.63%";
    show1.textColor = [UIColor grayColor];
    show1.font = [UIFont systemFontOfSize:14];
    show1.textAlignment = NSTextAlignmentLeft;
    [_showView addSubview:show1];
    UILabel * show2 = [[UILabel alloc] init];
    show2.frame = CGRectMake(10, CGRectGetMaxY(show1.frame) + 20 * px, topView.width-10, 20);
    show2.text = @"还款次数费=1.0元/次";
    show2.textColor = [UIColor grayColor];
    show2.font = [UIFont systemFontOfSize:14];
    show2.textAlignment = NSTextAlignmentLeft;
    [_showView addSubview:show2];
    
    UIView *line = [[UIView alloc]init];
    line.frame =CGRectMake(0,CGRectGetMaxY(show2.frame) + 40 , topView.width, 1);
    line.backgroundColor = WYCgrayColor;
    [_showView addSubview:line];
    
    UIButton * mainbutton = [[UIButton alloc] init];
    mainbutton.frame = CGRectMake(0, _showView.height - 150 * px, _showView.width, 40);
    [mainbutton setTitle:@"我知道了" forState:UIControlStateNormal];
    [mainbutton setTitleColor:wycMain forState:UIControlStateNormal];
    [mainbutton addTarget:self action:@selector(knowBtn) forControlEvents:UIControlEventTouchUpInside];
    mainbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_showView addSubview:mainbutton];
    
}
-(void)knowBtn
{
    _introView.frame =CGRectMake(0, 0, 0, 0);
    _showView.frame = CGRectMake(0, 0, 0, 0);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    repayModel * head = self.dataArray[section];
    return  head.dataArray.count ;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}
- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    repayModel * head = self.dataArray[section];

    return head.date;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * strID = @"cell";
    
   repayPlanViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];

    if (cell == nil)
    {

        cell=[[[NSBundle mainBundle]loadNibNamed:@"repayPlanViewCell" owner:self options:nil]lastObject];

    }
    
    repayModel * model = self.dataArray[indexPath.section];
    amountModel * DetailModel = model.dataArray[indexPath.row];
    
    cell.consumeDateLb.text = DetailModel.consumeDate;
    cell.poundageLb.text = [NSString stringWithFormat:@"手续费:￥%.1f",[DetailModel.poundage doubleValue]];
  
    NSString  *tye = DetailModel.type;
    //消费
    if ([tye isEqualToString:@"1"]) {
        cell.typeImg.image = [UIImage imageNamed:@"buy"];
         cell.planAmountLb.text =  [NSString stringWithFormat:@"￥%.1f",  [DetailModel.planAmount doubleValue ]+[DetailModel.poundage doubleValue]];
    }
    else
    {
        cell.typeImg.image = [UIImage imageNamed:@"borro"];
 cell.planAmountLb.text =  [NSString stringWithFormat:@"￥%.1f",  [DetailModel.planAmount doubleValue ]-[DetailModel.poundage doubleValue]];
    }

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(void)loadplan
{
    [HttpTool POST:epaymentplan params:_parmDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * result = responseObject[@"result"];
        _planNo =  [result objectForKey:@"planNo"];
            
        _Repaymentlabel1.text=  [NSString stringWithFormat:@"￥%.2f元",[result[@"consumePoundage"] doubleValue]+[result[@"repaymentPoundage"]doubleValue]];
         _Statement1.text = [NSString stringWithFormat:@"￥%@元",result[@"amount"]];
            
            NSArray * dataArray = result[@"planDetail"];
            for (NSDictionary * dic in dataArray) {
                
                repayModel * taranLationModel = [repayModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:taranLationModel];
                NSArray * detailsArray = dic[@"detail"];
                NSMutableArray * arr = [[NSMutableArray alloc] init];
                for (NSDictionary * detailsDic in detailsArray) {
                    
                    amountModel * DetailModel = [amountModel mj_objectWithKeyValues:detailsDic];
                    [arr addObject:DetailModel];
                }
                taranLationModel.dataArray = arr;
            }
          
            
        }
        [self.tableView reloadData];

        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
